package com.monkeygroup.mygoplus.view.home.activity.person;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.base.BaseActivity;
import com.monkeygroup.mygoplus.common.utils.LocalInfo;
import com.monkeygroup.mygoplus.common.utils.StringUtil;
import com.monkeygroup.mygoplus.common.utils.VilidateUtil;
import com.monkeygroup.mygoplus.http.HttpModel;
import com.monkeygroup.mygoplus.http.RequestUrl;
import com.monkeygroup.mygoplus.user.vo.PersonalNomalInfo;
import com.monkeygroup.mygoplus.view.home.activity.PublishedChooseAddrActivity;

import java.util.HashMap;
import java.util.Map;

/**
 * 修改姓名
 * Created by cumt on 2017/6/28.
 */

public class PersonChangeNameActivity extends BaseActivity implements View.OnClickListener {



    public static void gotoActivity(Activity activity,String name) {
        Intent intent = new Intent(activity,
                PersonChangeNameActivity.class);
        intent.putExtra("name",name);
        activity.startActivityForResult(intent,100);
    }


    private ImageView imgBack;
    private TextView txtTitle;
    private TextView btn_change_name_ok;

    private EditText et_new_name;

    //請求參數
    private Map<String, String> params;
    private HttpModel httpModel;

    private String name;

    @Override
    public void onClick(View v) {

    }

    @Override
    public void setContentView() {
        setContentView(R.layout.person_change_name);
    }

    @Override
    public void initView() {

        imgBack = (ImageView) findViewById(R.id.img_back);
        txtTitle = (TextView) findViewById(R.id.txt_title);
        txtTitle.setText("姓名");

        et_new_name = (EditText)findViewById(R.id.et_new_name);

        btn_change_name_ok= (TextView)findViewById(R.id.btn_change_name_ok);

        Intent intent = this.getIntent();
        Bundle bundle =  intent.getExtras();
        name = bundle.getString("name");

        et_new_name.setText(name);
        PersonalNomalInfo user = LocalInfo.getUserInfo(PersonChangeNameActivity.this);
        params = new HashMap<>();
        params.put("mobile",user.getMobile());

        //
        TextWatcher cname=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                params.put("name",String.valueOf(et_new_name.getText()));

            }

            @Override
            public void afterTextChanged(Editable s) {



            }
        };

        et_new_name.addTextChangedListener(cname);

        initRequest();

    }

    @Override
    public void setListener() {



    }

    @Override
    public void doEvent() {

        imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        btn_change_name_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                httpModel.startRequest();
            }
        });

    }
    private  void initRequest() {
        httpModel = new HttpModel(RequestUrl.baseUrl+RequestUrl.updateUserUrl, params, new HttpModel.SimpleOnHttpRequestListener() {
            @Override
            public void onRequestFinished(int resultCode, String resultMsg, Object data) {

                if (VilidateUtil.isSuccess(resultCode)) {

                    final Intent intent=new Intent();
                    intent.putExtra("name", String.valueOf(params.get("name")));
                    Toast.makeText(PersonChangeNameActivity.this,"修改成功", Toast.LENGTH_SHORT).show();
                    PersonChangeNameActivity.this.setResult(RESULT_OK,intent);
                    finish();
                }
                else{
                    Toast.makeText(PersonChangeNameActivity.this,"修改失败", Toast.LENGTH_SHORT).show();
                }

            }
        },true);
    }

}
